//这是vuex单独一个模块的完整代码  
const getAuth = {
	WRITE_EXTERNAL_STORAGE: false,
	READ_EXTERNAL_STORAGE: false,
	CALL_PHONE: false,
	isIos: typeof plus !== 'undefined' && plus.os && plus.os.name == "iOS",
	mapping: {
		'WRITE_EXTERNAL_STORAGE': {
			title: "存储空间/照片/摄像头权限说明",
			content: "便于您使用该功能上传您的照片/图片/及用于上传救援图片、上传 、下载、完善订单信息等场景中读取和写入相册和文件内容。",
			methods: 'SET_WRITE_EXTERNAL_STORAGE'
		},
		'READ_EXTERNAL_STORAGE': {
			title: "存储空间/照片/摄像头权限说明",
			content: "便于您使用该功能上传您的照片/图片/及用于上传救援图片、上传 、下载、完善订单信息、下载、读取和写入相册和文件内容。",
			methods: 'SET_READ_EXTERNAL_STORAGE'
		},
		'CALL_PHONE': {
			title: "拨打/管理电话权限说明",
			content: "便于您使用该功能联系商家或者商家与您联系等场景",
			methods: 'SET_CALL_PHONE'
		},
		'ACCESS_FINE_LOCATION': {
			title: "获取您的位置信息权限说明",
			content: "便于您使用该功能获取您位置所在的附近门店及显示距离等场景",
			methods: 'ACCESS_FINE_LOCATION'
		},
		'RECORD_AUDIO': {
			title: "获取您的麦克风权限说明",
			content: "便于参加会议时语音沟通及用于视频聊天场景中沟通",
			methods: 'RECORD_AUDIO'
		},
		'CAMERA': {
			title: "获取您的摄像头权限说明",
			content: "便于参加会议时及用于更换头像场景中获取摄像头内容",
			methods: 'CAMERA'
		},

	},
	async requestPermissions(permissionID) {
		// 检查是否在APP环境下
		if (typeof plus === 'undefined') {
			console.log('当前环境不支持plus对象，跳过权限请求');
			return new Promise((resolve) => {
				setTimeout(() => {
					resolve(1)
				}, 100)
			});
		}

		if (uni.getSystemInfoSync().platform === 'android') {
			try {
				console.log('获取权限中', permissionID);
				return new Promise(async (resolve, reject) => {
					console.log('android.permission.' + permissionID);
					if (this.isIos) {
						resolve(1)
						return
					}
					if (!this[permissionID]) {
						var viewObj = await this.nativeObjView(permissionID);
						console.log('viewObj', viewObj);
						viewObj.show();
					}
					// Android权限查询  
					function requestAndroidPermission(permissionID_) {
						return new Promise((resolve, reject) => {
							plus.android.requestPermissions(
								[
									permissionID_
								], // 理论上支持多个权限同时查询，但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装  
								function(resultObj) {
									console.log('resultObj', resultObj);
									var result = 0;
									for (var i = 0; i < resultObj.granted.length; i++) {
										var grantedPermission = resultObj.granted[i];
										console.log('已获取的权限：' + grantedPermission);
										result = 1
									}
									for (var i = 0; i < resultObj.deniedPresent
										.length; i++) {
										var deniedPresentPermission = resultObj
											.deniedPresent[
												i];
										console.log('拒绝本次申请的权限：' +
											deniedPresentPermission);
										result = 0
									}
									for (var i = 0; i < resultObj.deniedAlways
										.length; i++) {
										var deniedAlwaysPermission = resultObj
											.deniedAlways[
												i];
										console.log('永久拒绝申请的权限：' +
											deniedAlwaysPermission);
										result = -1
									}
									resolve(result);
								},
								function(error) {
									console.log('申请权限错误：' + error.code + " = " + error
										.message);
									resolve({
										code: error.code,
										message: error.message
									});
								}
							);
						});
					}

					const result = await requestAndroidPermission(
						'android.permission.' + permissionID
					);
					if (result === 1) {
						//'已获得授权'  
						console.log('已获取授权');
						this.mapping[permissionID].methods = true
					} else if (result === 0) {
						//'未获得授权'  
						this.mapping[permissionID].methods = false
					} else {
						this.mapping[permissionID].methods = true
						uni.showModal({
							title: '提示',
							content: '操作权限已被拒绝，请手动前往设置',
							confirmText: "立即设置",
							success: (res) => {
								if (res.confirm) {
									this.gotoAppPermissionSetting()
								}
							}
						})
					}
					if (viewObj) viewObj.close()
					resolve(result);
				});
			} catch (error) {
				console.log(error);
				reject(error);
			}
		} else {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					resolve(1)
				}, 100)
			})
		}

	},
	//提示框  
	nativeObjView(permissionID) {
		// 检查plus对象是否存在
		if (typeof plus === 'undefined') {
			console.log('当前环境不支持plus对象');
			return {
				show: () => {},
				close: () => {}
			};
		}

		const systemInfo = uni.getSystemInfoSync();
		const statusBarHeight = systemInfo.statusBarHeight;
		const navigationBarHeight = systemInfo.platform === 'android' ? 48 :
			44; // Set the navigation bar height based on the platform  
		const totalHeight = statusBarHeight + navigationBarHeight;
		let view = new plus.nativeObj.View('per-modal', {
			top: '0px',
			left: '0px',
			width: '100%',
			backgroundColor: '#444',
			//opacity: '.9'   
		})
		view.drawRect({
			color: '#fff',
			radius: '5px'
		}, {
			top: totalHeight + 'px',
			left: '5%',
			width: '90%',
			height: "100px",
		})
		view.drawText(this.mapping[permissionID].title, {
			top: totalHeight + 5 + 'px',
			left: "8%",
			height: "30px"
		}, {
			align: "left",
			color: "#000",
		}, {
			onClick: function(e) {
				console.log(e);
			}
		})
		view.drawText(this.mapping[permissionID].content, {
			top: totalHeight + 35 + 'px',
			height: "60px",
			left: "8%",
			width: "84%"
		}, {
			whiteSpace: 'normal',
			size: "14px",
			align: "left",
			color: "#656563"
		})

		function show() {
			view = plus.nativeObj.View.getViewById('per-modal');
			view.show()
			view = null //这里要为null，不然隐藏不了，不知道为啥  
		}

		function close() {
			view = plus.nativeObj.View.getViewById('per-modal');
			view.close();
			view = null
		}
		return {
			show,
			close
		}
	},

	// 跳转到**应用**的权限页面  
	gotoAppPermissionSetting() {
		// 检查plus对象是否存在
		if (typeof plus === 'undefined') {
			console.log('当前环境不支持plus对象，无法跳转权限设置');
			return;
		}

		if (this.isIos) {
			var UIApplication = plus.ios.import("UIApplication");
			var application2 = UIApplication.sharedApplication();
			var NSURL2 = plus.ios.import("NSURL");
			// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");       
			var setting2 = NSURL2.URLWithString("app-settings:");
			application2.openURL(setting2);

			plus.ios.deleteObject(setting2);
			plus.ios.deleteObject(NSURL2);
			plus.ios.deleteObject(application2);
		} else {
			// console.log(plus.device.vendor);  
			var Intent = plus.android.importClass("android.content.Intent");
			var Settings = plus.android.importClass("android.provider.Settings");
			var Uri = plus.android.importClass("android.net.Uri");
			var mainActivity = plus.android.runtimeMainActivity();
			var intent = new Intent();
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
			intent.setData(uri);
			mainActivity.startActivity(intent);
		}
	}
}
export default getAuth;